# Copyright 2012 Brett Ponsler
# This file is part of siriviewer.
#
# siriviewer is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# siriviewer is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with siriviewer.  If not, see <http://www.gnu.org/licenses/>.
''' '''
from PyQt4 import QtGui, QtCore

from pyamp.ui.qt import BaseWidget
from pyamp.util.dictionary import getItems


class BaseObject(BaseWidget):
    ''' '''
    def __init__(self, controller, data, width, height):
        ''' '''
        self.data = data if data is not None else {}
        self.width, self.height = (width, height)
        self.setMargins(10, 0, 10, 5)
        BaseWidget.__init__(self, controller=controller)

    def sizeHint(self):
        ''' '''
        # @todo: not sure why we need to add 12 here to make it work with
        #        the top and bottom images...
        return QtCore.QSize(400, QtGui.QWidget.sizeHint(self).height() + 12)

    def getProperty(self, propertyName, default=None):
        ''' '''
        text = None

        props = self.data.get('properties')
        if props is not None:
            text = props.get(propertyName, default)

        return text

    def getItems(self, items, default=None, data=None):
        ''' '''
        data = self.data if data is None else data
        return getItems(data, items, default)

    def createWidget(self, objType):
        '''Create a new widget of the given type.

        * objType -- The type of object to create

        '''
        from siriviewer.objects.factory import Factory
        return Factory.create(self.controller, objType, self.width, self.height)
